package com.angus.common.security.config;

import com.paypal.base.rest.APIContext;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liyanan
 * @date 2025/03/03 10:05 PM
 * @description
 */
@Data
@ConfigurationProperties(prefix = "paypal")
public class PayPalConfig {
    private static final Logger logger = LoggerFactory.getLogger(PayPalConfig.class);
    private static final String SCOPE = "payments"; // 明确声明权限
    private String clientId;
    private String clientSecret;
    private String mode;

    private String cancelUrl;
    private String successUrl;
    @Bean
    public APIContext apiContext() {
        // 获取访问令牌
        logger.info("clientId: {}, clientSecret: {}, mode: {}", this.clientId, this.clientSecret, this.mode);
        APIContext apiContext = new APIContext( this.clientId, this.clientSecret, this.mode, this.getPayPalConfig());
        apiContext.getAccessToken();
        logger.info("apiContext: {}, accessToken: {}", apiContext, apiContext.getAccessToken());
        return apiContext;
    }

    /**
     * 设置 PayPal 的基本配置（如 mode）
     */
    private Map<String, String> getPayPalConfig() {
        Map<String, String> configMap = new HashMap<>();
        configMap.put("mode", this.mode);
        configMap.put("paypal.sdk.scope", this.SCOPE);
        return configMap;
    }
}
